package com.yw.atp.util;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Constants {
	private static Properties p = new Properties();
	private static Map<String,String> sqlMap;
	public static String PUSH_URL = "http://61.172.246.21:7070/push.do";
	/**
	 * 重要通告未读状态
	 */
	public final static String IMPORT_NOTICE_UN_READ = "0";
	/**
	 * 重要通告已读状态
	 */
	public final static String IMPORT_NOTICE_READED = "1";
	public final static String COMMAND_STATUS_NEW = "0";
	public final static String COMMAND_STATUS_DEL = "1";
	public final static String COMMAND_STATUS_APCL = "2";
	public final static String COMMAND_STATUS_YCL = "3";
	public final static String COMMAND_STATUS_YZF = "4";
	
	static{
		 try {
			InputStream in = Constants.class.getResourceAsStream("/polling.properties");
			p.load(in);
		 } catch (Exception e) {
			e.printStackTrace();
		 }
		 try {
			  InputStream xmlIn = Constants.class.getResourceAsStream("/eipc_sqls.xml");
			  parserXml(xmlIn);
		 } catch (Exception e) {
			  e.printStackTrace();
		 }
	  }
	  
	  public static String getSql(String id){
		  //System.out.println(String.valueOf(sqlMap)+"-------constentKey " + id +" = "+ sqlMap.containsKey(id));
		  String sql = sqlMap.get(id);
		  return sql;
	  }
	
	  public static String get(String key,String def){
		  return p.getProperty(key,def);
	  }
	  
	  public static void parserXml(InputStream in) { 
		  try { 
			  sqlMap = new HashMap<String,String>();
			  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			  DocumentBuilder db = dbf.newDocumentBuilder(); 
			  Document document = db.parse(in);
			  NodeList nl = document.getElementsByTagName("sql");
			  for (int i = 0; i < nl.getLength(); i++) {
				  Node n = nl.item(i);
				  //System.out.println("------------------------- attributes :" +n.getNodeName()+"-:-"+n.getFirstChild().getNodeValue().replaceAll("\r\n"," ").replaceAll("\n"," "));
				  NamedNodeMap nnm = n.getAttributes();
				  for(int j=0;j<nnm.getLength();j++){
					  //System.out.println(nnm.item(j).getNodeName()+" equals id = "+String.valueOf("id".equals(nnm.item(j).getNodeName())));
					  if("id".equals(nnm.item(j).getNodeName())){
						  sqlMap.put(nnm.item(j).getNodeValue(),n.getFirstChild().getNodeValue().replaceAll("\r\n"," ").replaceAll("\n"," "));
						  //System.out.println("------------------------- attributes :" + nnm.item(j).getNodeName()+"-:-"+nnm.item(j).getNodeValue());
					  }
				  }
			  }
		  }catch(Exception e){
			  e.printStackTrace();
		  }
	  }
	  
//	  public static void main(String[] args) throws Exception {
//		  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//		  DocumentBuilder db = dbf.newDocumentBuilder(); 
//		  File file = new File("/home/jtao/workspace/atp/trunk/ipc-www/atp/src/main/resources/eipc_sqls.xml");
//		  System.out.println("------------------------- file :" + file);
//		  InputStream in = new FileInputStream(file);
//		  Document document = db.parse(in);
//		  NodeList nl = document.getElementsByTagName("sql");
//		  for (int i = 0; i < nl.getLength(); i++) {
//			  Node n = nl.item(i);
//			  System.out.println("------------------------- attributes :" +n.getNodeName()+"-:-"+n.getFirstChild().getNodeValue().replaceAll("\r\n"," ").replaceAll("\n"," "));
//			  NamedNodeMap nnm = n.getAttributes();
//			  for(int j=0;j<nnm.getLength();j++){
//				  System.out.println("------------------------- attributes :" + nnm.item(j).getNodeName()+"-:-"+nnm.item(j).getNodeValue());
//			  }
//		  }
//		  System.out.println("------------------------- sql node length :" + document.getElementsByTagName("sql").getLength());
//		  Element  el = document.getElementById("match_scroe");
//		  System.out.println("------------------------- child length :" + el.getChildNodes().getLength());
//		  String sql = el.getFirstChild().getNodeValue();
//		  System.out.println("------------------------- sql :" + sql);
//	}
}
